package com.link.base.base.file.controller;

import com.link.base.base.file.model.HeadRowStructure;
import com.link.base.base.file.service.ExpAndImpService;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.model.ReturnCodeConstants;
import com.link.core.cllog.LCLogger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 头行结构导入导出controller
 *
 * @author LiYang
 * @date 20191025
 */
@Controller
@RequestMapping("/link/file")
public class ExpAndImpController {

    @Resource
    ExpAndImpService expAndImpService;

    /**
     * 导出头行
     * 全部数据一次性导出，如果数据量非常大可能会存在内存溢出等问题，不适合对大数量的头行结构进行导出，例如导出环境全部的订单头行
     *
     * @author LiYang
     * @date 20191025
     * @param entity
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/exportHeadAndRow")
    @ResponseBody
    public Map<String, Object> exportHeadAndRow(@JsonParam HeadRowStructure entity, HttpSession session,
                                          HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<>(4);
        try {
            String downloadUrl = expAndImpService.exportData(entity, request);
            result.put("success", true);
            result.put("url", downloadUrl);
            result.put("code", ReturnCodeConstants.SUCCESS);
        } catch (Exception e) {
            LCLogger.error().withMessageKey("exportHeadAndRow").withMessage(e.getMessage());
            result.put("success", false);
            result.put("result", e.getMessage());
            result.put("code", ReturnCodeConstants.EXCEPTION);
        }
        return result;
    }

    /**
     * 导入头行
     * 全部数据一次性导入，如果数据量非常大可能会存在内存溢出等问题，不适合对大数量的头行结构进行导入，例如导入环境全部的订单头行
     *
     * @author LiYang
     * @date 20191025
     * @param entity
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importHeadAndRow")
    @ResponseBody
    public Map<String, Object> importHeadAndRow(@JsonParam HeadRowStructure entity, HttpSession session,
                                                HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<>(4);
        try {
            boolean success = expAndImpService.importData(entity, request);
            if (success) {
                result.put("success", true);
                result.put("code", ReturnCodeConstants.SUCCESS);
            } else {
                result.put("success", false);
                result.put("code", ReturnCodeConstants.EXCEPTION);
            }
        } catch (Exception e) {
            LCLogger.error().withMessageKey("exportHeadAndRow").withMessage(e.getMessage());
            result.put("success", false);
            result.put("result", e.getMessage());
            result.put("code", ReturnCodeConstants.EXCEPTION);
        }
        return result;
    }
}
